package com.itheima.ydd.controller;


import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.mapper.YddTagMapper;
import com.itheima.ydd.service.IYddCateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * //分类管理业务层 ---》
 */
@RestController
@RequestMapping("/ydd_cate")
@Slf4j
public class YddCateController {
    @Autowired
    private IYddCateService yddCateService;

    /**
     * 添加分类
     * @param yddCate
     * @return
     */
    @PostMapping("/add")
    public ResultData save(@RequestBody YddCate yddCate){
        log.info(yddCate.toString());
        if (StringUtils.isNotEmpty(yddCate.getCatename())&&StringUtils.isNotEmpty(yddCate.getIcon())) {
            yddCateService.save(yddCate);
        }
        return ResultData.ok("","");
    }
    /**
     * 分页查找
     * @param catename
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData page(String catename, Integer page , Integer limit){
        log.info("page={},pageSize={},name={}",page,limit,catename);
        return yddCateService.getYddcatePage(catename,page,limit);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData delete(Integer id){
        log.info("待删除的id为{}",id);
        //判断当前分类管理是否被关联
        yddCateService.remove(id);
        //categoryService.removeById(id);
        return  ResultData.ok("","");
    }

    /**
     * 数据详情(回显)
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData getById(Long id){
        YddCate yddCate = yddCateService.getById(id);
        if (yddCate != null) {
            return ResultData.ok("",yddCate);
        }
        return ResultData.error("未找到该分类数据");
    }


    /**
     * 修改分类
     * @param yddCate
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddCate yddCate){
        log.info(yddCate.toString());
        yddCateService.updateById(yddCate);
        return ResultData.ok("","");
    }

    /**
     * 查询所有分类
     * @return
     */
    @GetMapping("/all")
    public ResultData allYddCate(){
        List<YddCate> list = yddCateService.list();
        log.info(list.toString());
        return ResultData.ok("",list);
    }
}
